package com.oig.sys.auth.service.impl;

import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.dao.ITbResourcePermissionDao;
import com.oig.sys.auth.dto.PermissionDto;
import com.oig.sys.auth.dto.ResourceDto;
import com.oig.sys.auth.entity.TbResourcePermission;
import com.oig.sys.auth.service.ITbPermissionService;
import com.oig.sys.auth.service.ITbResourcePermissionService;
import com.oig.sys.auth.vo.PermissionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class TbResourcePermissionServiceImpl implements ITbResourcePermissionService {

    private final ITbResourcePermissionDao resourcePermissionDao;
    private final ITbPermissionService permissionService;

    @Autowired
    public TbResourcePermissionServiceImpl(ITbResourcePermissionDao resourcePermissionDao,
                                           ITbPermissionService permissionService){
        this.resourcePermissionDao = resourcePermissionDao ;
        this.permissionService = permissionService;
    }

    @Override
    public List<PermissionVo> findPermissionId(Integer resourceId) {
        List<Integer> permissionIdList = this.resourcePermissionDao.findPermissionIdByResourceIdList(new ArrayList<Integer>(){{add(resourceId);}});
        return permissionService.getAllPermission(permissionIdList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delByResourceId(Integer resourceId) {
        List<Integer> delIdList = this.resourcePermissionDao.findAllIdByResourceId(resourceId);
        if (delIdList!=null && !delIdList.isEmpty()){
            this.resourcePermissionDao.deleteByIdList(delIdList);
        }
    }

    @Override
    public List<Integer> findPermissionId(List<ResourceDto> resourceDtoList) {
        List<Integer> resourceIdList = new ArrayList<>();
        for (ResourceDto resourceDto : resourceDtoList){
            if (resourceDto.getId()==null){
                throw new BadRequestException("功能id为空");
            }
            resourceIdList.add(resourceDto.getId());
        }
        return this.resourcePermissionDao.findPermissionIdByResourceIdList(resourceIdList) ;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<PermissionVo> addResourcePermission(Integer resourceId, Integer systemId, List<PermissionDto> permissionDtoList) {
        List<PermissionVo> permissionVoList = new ArrayList<>();
        TbResourcePermission resourcePermission = new TbResourcePermission();
        //支持没有存的接口直接添加
        for (PermissionDto permissionDto : permissionDtoList) {
            permissionDto.setSystemId(systemId);
            PermissionVo permissionVo = this.permissionService.addOrUpdatePermission(permissionDto);
            if (permissionVo!=null){
                permissionVoList.add(permissionVo);
                resourcePermission.setPermissionId(permissionVo.getId());
                resourcePermission.setResourceId(resourceId);
                this.resourcePermissionDao.save(resourcePermission) ;
            }
        }
        return permissionVoList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<PermissionVo> updateResourcePermission(Integer resourceId, Integer systemId, List<PermissionDto> permissionDtoList) {
        List<Integer> idList = new ArrayList<>();
        List<PermissionVo> permissionVoList = new ArrayList<>();
        for (PermissionDto permissionDto : permissionDtoList) {
            permissionDto.setSystemId(systemId);
            PermissionVo permissionVo = this.permissionService.addOrUpdatePermission(permissionDto);
            if (permissionVo==null){
                continue;
            }
            TbResourcePermission resourcePermission = this.resourcePermissionDao.getByResourceIdAndPermissionId(resourceId, permissionVo.getId());
            if (resourcePermission==null){
                resourcePermission = new TbResourcePermission();
                resourcePermission.setPermissionId(permissionVo.getId());
                resourcePermission.setResourceId(resourceId);
                this.resourcePermissionDao.save(resourcePermission) ;
            }
            permissionVoList.add(permissionVo) ;
            idList.add(resourcePermission.getId()) ;
        }
        List<Integer> ids = resourcePermissionDao.findResourcePermissionExIds(resourceId, idList);
        if (ids!=null && !ids.isEmpty()){
            this.resourcePermissionDao.deleteByIdList(ids);
        }
        return permissionVoList;
    }



}
